package org.itboys.mobile.service;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.wallet.CooWalletMapper;
import org.itboys.mobile.dao.wallet.CooWalletRecordMapper;
import org.itboys.mobile.entity.mysql.wallet.CooWallet;
import org.itboys.mobile.entity.mysql.wallet.CooWalletRecord;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/2/4
 * 描述：
 */
public class SundayJobService {
    @Autowired
    private CooWalletRecordMapper walletRecordMapper;
    @Autowired
    private CooWalletMapper walletMapper;
    public void release(){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("frozenAmount",1);
        List<CooWallet> wallets=walletMapper.select(param);
        if(ListUtil.isNotNull(wallets)){
            for (CooWallet thisWallet :wallets) {
                BigDecimal scaleAmount=thisWallet.getFrozenAmount();
                BigDecimal zero=new BigDecimal("0.000");
                //如果当前可用额度大于奖励   则加当前奖励    减少可用额度
                if(thisWallet.getAbleLimit().compareTo(scaleAmount)!=-1){
                    thisWallet.setAbleLimit(thisWallet.getAbleLimit().subtract(scaleAmount));
                    thisWallet.setTotalAmount(thisWallet.getTotalAmount().add(scaleAmount));
                    thisWallet.setCashAmount(thisWallet.getCashAmount().add(scaleAmount.multiply(new BigDecimal("0.7"))));
                    thisWallet.setScoreAmount(thisWallet.getScoreAmount().add(scaleAmount.multiply(new BigDecimal("0.3"))));
                    CooWalletRecord walletRecord =new CooWalletRecord(thisWallet.getMemberId(),  thisWallet.getId(), SundayCommonConstants.wallet_record_type_manager, "每天释放奖励", thisWallet.getAbleLimit(),scaleAmount, SundayCommonConstants.YES);
                    walletRecordMapper.insert(walletRecord);
                    //如果可用额度小于当前的奖励则取完所有可用额度    作为总奖励
                }else if(thisWallet.getAbleLimit().compareTo(scaleAmount)==-1&&thisWallet.getAbleLimit().compareTo(zero)==1){
                    thisWallet.setAbleLimit(zero);
                    thisWallet.setTotalAmount(thisWallet.getTotalAmount().add(thisWallet.getAbleLimit()));
                    thisWallet.setCashAmount(thisWallet.getCashAmount().add(thisWallet.getAbleLimit().multiply(new BigDecimal("0.7"))));
                    thisWallet.setScoreAmount(thisWallet.getScoreAmount().add(thisWallet.getAbleLimit().multiply(new BigDecimal("0.3"))));
                    CooWalletRecord walletRecord =new CooWalletRecord(thisWallet.getMemberId(),  thisWallet.getId(), SundayCommonConstants.wallet_record_type_manager, "每天释放奖励", thisWallet.getAbleLimit(),scaleAmount, SundayCommonConstants.YES);
                    walletRecordMapper.insert(walletRecord);
                }
                walletMapper.update(thisWallet);
            }
        }

    }
    public static void main(String[] args) {
    }

}
